home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_06 / letters / noprecis.c < prev   
C/C++ Source or Header  |  1995-04-17  |  987b  |  40 lines

  1. // The computer lie.
  2. // Joe McCarty
  3. // The problem with expressing an infinite
  4. // quantity of numbers in a finite machine.
  5. // Look at the program and predict the results.
  6. // Then try running the program.
  7.  
  8. #include <stdio.h>
  9. #include <math.h>
  10.  
  11. void test(int flag){
  12. double x=4.999999999;
  13. int exp=0;
  14. printf("x= %20.18f\n",x);        //The original number
  15. while(x<1000.0){x=x*10.0;exp--;} //Adjust for truncation
  16.                                  //tion or rounding
  17. while(x>10000.0){x=x/10;exp++;}
  18. if(flag)x=(double)(int)x;       //Truncate
  19. if(!flag)x=(double)(int)(x+.5); //Round
  20. printf("x= %20.18f\n",x);       //Show result
  21. x=x*pow(10.0,exp);              //Back to original exponent
  22. printf("x= %20.18f\n\n",x);       //Show result
  23. }//end test
  24.  
  25. void main(){
  26.  test(0);
  27.  test(1);
  28. }//end main
  29.  
  30. /*
  31. x= 4.999999998999999920
  32. x= 5000.000000000000000000
  33. x= 5.000000000000000000
  34.  
  35. x= 4.999999998999999920
  36. x= 4999.000000000000000000
  37. x= 4.998999999999999670
  38. */ 
  39.  
  40.